package com.util.library.sms;

import java.util.ArrayList;
import java.util.List;

import com.hethongviet.library.R;
import com.util.library.common.ConfigLib;
import com.util.library.common.Utils;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.view.View;

public final class SMSHelper
{
	private static String phoneNumberDevice = null;

	/**
	 * the activity is activity and OnListenerSMSMessageFail
	 * */
	private static Activity activity = null;

	private static int countMessageId = 0;

	private static String getMessageId()
	{
		String id = "";
		if (countMessageId < 10)
		{
			id = "00" + countMessageId++;
		}
		else if (countMessageId < 100)
		{
			id = "0" + countMessageId++;
		}
		else
		{
			id = "" + countMessageId;
			if (countMessageId == 999)
			{
				countMessageId = 0;
			}
			else
				countMessageId++;

		}

		return id;
	}

	private static List<Transaction> transactionList = new ArrayList<Transaction>();

	public static Transaction solve(String messageReceive)
	{
		Utils.debug("Get the Transaction....from " + transactionList.size());
		String id = getIdCode(messageReceive);

		if (id == null || id.equals(""))
		{
			Utils.debug("ERROR----- null ID!");
			return null;
		}

		for (Transaction transaction : transactionList)
		{
			Utils.debug("--search transaction.getId() = " + transaction.getId());

			if (transaction.getId().equals(id))
			{
				if (transaction.isFail())
				{
					Utils.debug("Fail this transaction!");
					return null;
				}

				Utils.debug("*** Transaction matched! = " + id);

				transaction.setMessageReceive(getContent(messageReceive,
						transaction.getCodeSend().length()));

				transaction.success();

				transactionList.remove(transaction);

				Utils.debug("last size = " + transactionList.size());

				return transaction;
			}
		}
		Utils.debug("Receive nothing!");
		return null;
	}

	public static String getDeviceNumber()
	{
		return phoneNumberDevice;
	}

	public static void setMessageId(int id)
	{
		countMessageId = id;
	}

	public static String getContent(String message, int codeLength)
	{
		String content = "";
		return content;
	}

	private static String getIdCode(String message)
	{
		String idCode = "";
		
		return idCode;
	}

	public static void setDeviceNumber(String number)
	{
		phoneNumberDevice = number;
	}

	public static void setActivity(Activity activity1)
	{
		activity = activity1;
	}

	
	// ---sends an SMS message to another device---
	public static void sendSMS(String number, String code, String message,
			int messageFailId, String idSent, View viewRelay, int timeOut)
	{
		final String CODE_SENT = "SMS_SENT";
		final String CODE_DELIVERED = "SMS_DELIVERED";

		PendingIntent sentPendingIntent = PendingIntent.getBroadcast(activity,
				0, new Intent(CODE_SENT), 0);

		PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(
				activity, 0, new Intent(CODE_DELIVERED), 0);

		// ---when the SMS has been sent---
		activity.registerReceiver(new BroadcastReceiver()
		{
			@Override
			public void onReceive(Context arg0, Intent arg1)
			{
				switch (getResultCode())
				{
				case Activity.RESULT_OK:
					Utils.toast(activity, R.string.message_SMS_send);
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Utils.toast(activity, R.string.message_error_SMS_send);
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Utils.toast(activity,
							R.string.message_error_SMS_not_available);
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Utils.toast(activity, R.string.message_error_SMS_send);
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Utils.toast(activity, R.string.message_error_SMS_radio_off);
					break;
				}
			}
		}, new IntentFilter(CODE_SENT));

		// ---when the SMS has been delivered---
		activity.registerReceiver(new BroadcastReceiver()
		{
			@Override
			public void onReceive(Context arg0, Intent arg1)
			{
				switch (getResultCode())
				{
				case Activity.RESULT_OK:
					Utils.toast(activity, R.string.message_SMS_deliveried);
					break;
				case Activity.RESULT_CANCELED:
					Utils.toast(activity,
							R.string.message_error_SMS_not_deliveried);
					break;
				}
			}
		}, new IntentFilter(CODE_DELIVERED));

		Transaction transaction = new Transaction(
				(OnSMSMessageFailListener) activity, timeOut,
				messageFailId, viewRelay);
		transaction.setId(getMessageId());
		transaction.setMessageSend(message);
		transaction.setCodeSend(code);
		transaction.setRecipient(number);

		if (!ConfigLib.IS_DEBUG_SMS_RECEIVE)
		{
			SmsManager sms = SmsManager.getDefault();
			String messageWillSend = Utils.isStringNull(idSent) ? transaction
					.getMessageWillSend() : transaction
					.getMessageWillSendNoID() + idSent;
			sms.sendTextMessage(number, null, messageWillSend,
					sentPendingIntent, deliveredPendingIntent);

			Utils.debug("---------Sending message via SMS = " + messageWillSend);
			Utils.debug("---------Sending message via SMS to " + number);
		}

	}
}
